<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onDisplayLeftAdminColumnHelpDesk', 'plgHelpDeskFeedbackstats' );

/**
 * HelpDesk Plugin
 *
 * @package		Joomla
 * @subpackage	JFramework
 * @since 		1.5
 */

	function plgHelpDeskFeedbackstats( ) {
		global $mainframe, $database;
		$success = false;
	
		$return = array();
		$return['avgfeedback_overall'] = 0;
		$return['avgfeedback_lastmonth'] = 0;
		$return['avgfeedback_thismonth'] = 0;
	
		$today = mosFormatDate( date("Y-m-d"), "%Y-%m-%d");
		$thismonth = mosFormatDate( date("Y-m-d"), "%Y-%m-01");
		
		$query = " SELECT DATE_SUB('".$thismonth."', INTERVAL 1 MONTH) ";
		$database->setQuery( $query );
		$lastmonth = $database->loadResult();
		
		$status_query = " AND ( t.status = '2' ) AND ( t.firstresponse_by > '0' ) ";
		
		// closed tix	  
		$query = "SELECT t.* FROM #__helpdesk_tickets as t "
			. " WHERE 1 "
			. " AND ( t.status = '2' ) "
			;	
		$database->setQuery( $query );
		$return["closedhelpdesk_all"] = $database->loadObjectList();
	
		// closed tix	  
		$query = "SELECT t.* FROM #__helpdesk_tickets as t "
			. " WHERE 1 "
			. $status_query
			;	
		$database->setQuery( $query );
		$return["closedhelpdesk"] = $database->loadObjectList();
		
		// their feedback
		$feedback_sum = "";
		if ($return["closedhelpdesk"]) { foreach ($return["closedhelpdesk"] as $r) {
				$feedback_sum = $feedback_sum + $r->feedback_rating;
			} 
			$return["avgfeedback_overall"] = $feedback_sum/count($return["closedhelpdesk"]);
		}
		
		
		// closed tix this month
		$query = "SELECT t.* FROM #__helpdesk_tickets as t "
			. " WHERE 1 "
			. $status_query
			. " AND t.closed_datetime >= '".$database->getEscaped( trim( strtolower( $thismonth ) ) )."' "
			. " AND t.closed_datetime <= NOW() "
			;	
		$database->setQuery( $query );
		$return["closedhelpdesk_thismonth"] = $database->loadObjectList();
		
		// their feedback
		$feedback_sum_thismonth = "";
		if ($return["closedhelpdesk_thismonth"]) { foreach ($return["closedhelpdesk_thismonth"] as $r) {
			$feedback_sum_thismonth = $feedback_sum_thismonth + $r->feedback_rating;
			} 
			$return["avgfeedback_thismonth"] = $feedback_sum_thismonth/count($return["closedhelpdesk_thismonth"]);
		}
		
	
		// closed tix last month
		$query = "SELECT t.* FROM #__helpdesk_tickets as t "
			. " WHERE 1 "
			. $status_query
			. " AND t.closed_datetime >= '".$database->getEscaped( trim( strtolower( $lastmonth ) ) )."' "
			. " AND t.closed_datetime < '".$database->getEscaped( trim( strtolower( $thismonth ) ) )."' "
			;	
		$database->setQuery( $query );
		$return["closedhelpdesk_lastmonth"] = $database->loadObjectList();
		
		// their feedback
		$feedback_sum_lastmonth = "";
		if ($return["closedhelpdesk_lastmonth"]) { foreach ($return["closedhelpdesk_lastmonth"] as $r) {
			$feedback_sum_lastmonth = $feedback_sum_lastmonth + $r->feedback_rating;
			} 
			$return["avgfeedback_lastmonth"] = $feedback_sum_lastmonth/count($return["closedhelpdesk_lastmonth"]);
		}
	
		$stats = $return;
		?>
		<div class='noteg'>
			<strong><?php echo HelpDeskText::_( 'Feedback Statistics' ); ?></strong>
			 
			<table class='invisible'>
            
			  <tr>
				<td>
					<?php
						$link = "index2.php?option=com_helpdesk&controller=tickets&task=list&status=closed";
						$num = count($stats["closedhelpdesk_all"]);
					?>
				</td>
				<td>
					<?php 
						echo "<a href='".$link."'>".$num." ".HelpDeskText::_( 'Closed Tickets' )."</a>"; 
					?>
				</td>
			  </tr>
              
			  <tr>
				<td>
					<?php
						$link = "index2.php?option=com_helpdesk&controller=tickets&task=list&status=closed";
						$num = count($stats["closedhelpdesk"]);
					?>
				</td>
				<td>
					<?php 
						echo "<a href='".$link."'>".$num." ".HelpDeskText::_( 'Closed with Response' )."</a>"; 
					?>
				</td>
			  </tr>
              
			  <tr>
				<td>
					<?php
						$link = "index2.php?option=com_helpdesk&controller=tickets&task=list&status=closed";
						$num = number_format($stats["avgfeedback_overall"], '1', '.', ',');
					?>
				</td>
				<td>
					<?php 
						echo "<a href='".$link."'>".HelpDeskText::_( 'Average Feedback' ).": ".$num."</a><br>"; 
						echo "<center>";
						echo "<a href='".$link."'>".HelpDeskHelperHelpDesk::getFeedbackRatingImage ( $num )."</a>";
						echo "</center>";						
					?>
				</td>
			  </tr>
                           
			  <tr>
				<td>
					<?php
						$link = "index2.php?option=com_helpdesk&controller=tickets&task=list&status=closed";
						$num = number_format($stats["avgfeedback_lastmonth"], '1', '.', ',');
					?>
				</td>
				<td>
					<?php 
						echo "<a href='".$link."'>".HelpDeskText::_( 'Last Month' ).": ".$num." (".count($stats["closedhelpdesk_lastmonth"]).")</a><br>"; 
						echo "<center>";
						echo "<a href='".$link."'>".HelpDeskHelperHelpDesk::getFeedbackRatingImage ( $num )."</a>";
						echo "</center>";						
					?>
				</td>
			  </tr>
              
			  <tr>
				<td>
					<?php
						$link = "index2.php?option=com_helpdesk&controller=tickets&task=list&status=closed";
						$num = number_format($stats["avgfeedback_thismonth"], '1', '.', ',');
					?>
				</td>
				<td>
					<?php 
						echo "<a href='".$link."'>".HelpDeskText::_( 'This Month' ).": ".$num." (".count($stats["closedhelpdesk_thismonth"]).")</a><br>"; 
						echo "<center>";
						echo "<a href='".$link."'>".HelpDeskHelperHelpDesk::getFeedbackRatingImage ( $num )."</a>";
						echo "</center>";						
					?>
				</td>
			  </tr>

              
			</table>
		</div>
		
		<?php
		$success = true;
		return $success;
		
	}